#ifndef __CINT__
#include <TGeoManager.h>
#include <TSystem.h>
#include <TStyle.h>
#include <TGeoMatrix.h>
#include <TGeoTrack.h>
#include <TEveManager.h>
#include <TEveLine.h>
#endif
void BuildGeometry(TString filename="CpGeom.root",Bool_t showGeometry=kTRUE) {
   gStyle->SetCanvasPreferGL(kTRUE);
   gSystem->Load("libGeom");
   TGeoManager *geom = new TGeoManager("simple1", "Simple geometry");
   TGeoMaterial *matVacuum = new TGeoMaterial("Vacuum", 0,0,0);
   TGeoMaterial *matVessel = new TGeoMaterial("Vessel", 0,0,0);
   TGeoMaterial *matMagnet = new TGeoMaterial("Magnet", 0,0,0);
   
   TGeoMedium *Vacuum = new TGeoMedium("Vacuum",1, matVacuum);
   TGeoMedium *Vessel = new TGeoMedium("Vessel",2, matVessel);
   TGeoMedium *Magnet = new TGeoMedium("Magnet",3, matMagnet);
   
   Double_t worldx = 2*1e-1;
   Double_t worldy = 2*1e-1;
   Double_t worldz = 2*1e-1;
   TGeoVolume *top = geom->MakeBox("TOP", Vacuum, worldx, worldy, worldz);
//    top->SetTransparency(100);
//    top->SetVisibility(kTRUE);
   geom->SetTopVolume(top);

   Double_t RVesel=1e-3;


   
   TGeoVolume *magnetVol = geom->MakeBox("MAGNET", Magnet,6*1e-2/2, 6*1e-2/2,4.85*1e-2);
   magnetVol->SetTransparency(50);
   magnetVol->SetLineColor(kGray);
   
   TGeoRotation *rotMagnet = new TGeoRotation("rot",90,90,0);
   TGeoCombiTrans *transRotMagnet = new TGeoCombiTrans(0/*-(3.5*1e-3+RVesel)*/, 0, 1e-1, rotMagnet);
//    TGeoCombiTrans *transRotMagnet = new TGeoCombiTrans(-4.25*1e-2, 0, 1e-1, rotMagnet);
   top->AddNode(magnetVol, 1, transRotMagnet);

//    TGeoVolume *vesselVol = geom->MakeTube("VESSEL1", Vessel, 0.9*RVesel,RVesel,1e-1);
   TGeoVolume *vesselVol = geom->MakeTube("VESSEL1", Vessel, 0,RVesel,1e-1);
   vesselVol->SetTransparency(30);
   vesselVol->SetLineColor(kRed);
   TGeoRotation *rotVessel = new TGeoRotation("rot",0,90,0);
   TGeoCombiTrans *transRotVessel = new TGeoCombiTrans(0,0/*-(3.5*1e-3+RVesel)*/,0, rotVessel);
   magnetVol->AddNode(vesselVol, 1, transRotVessel);
   magnetVol->SetVisibility(kTRUE);
   geom->CloseGeometry();
   geom->Export(filename.Data());

   TGeoManager::Import(filename.Data());

   if (showGeometry) ShowEvent();
}

void ShowEvent() {


   TEveManager::Create();
   
   TGeoNode* node = gGeoManager->GetTopNode();
   TEveGeoTopNode* en = new TEveGeoTopNode(gGeoManager, node);
   en->SetVisLevel(2);
   en->GetNode()->GetVolume()->SetVisibility(kFALSE);
   
   gEve->AddGlobalElement(en);


   TEveCompound* cmp = new TEveCompound;
   cmp->SetMainColor(kGreen);
   gEve->AddElement(cmp);
   cmp->OpenCompound();
   
   Double_t step = 1e-2;
//   step=1;
//    gGeoManager->SetStep(step);

   Double_t initPiont[] = {0.0,0.0,0};
   Double_t initDirection[] = {0.0,0.0,1.0};
   gGeoManager->InitTrack(initPiont,initDirection);
   
   const Double_t *xyz;
   TGeoNode*nextNode;
   const Double_t *currentDirection;
   Double_t *currentNodeMatrix;
   TVector3 vCurrentDirection, vCurrentPoint;
   TVector3 vNormal;
   
   TEveLine *line = new TEveLine;
   line->SetMainColor(kYellow);
   
   
   const Double_t *glob_pt = gGeoManager->GetCurrentPoint();
   Double_t local_pt[3];
   Double_t my_pt[3];

   my_pt[0]=0.0;my_pt[1]=0.0;my_pt[2]=1e-1;
   // 1st step
   gGeoManager->SetCurrentPoint(my_pt[0],my_pt[1],my_pt[2]);
   line->SetNextPoint(my_pt[0],my_pt[1],my_pt[2]);
   nextNode = gGeoManager->Step(kFALSE);
   if (!gGeoManager->IsOutside()) Printf("%s",nextNode->GetName());
   gGeoManager->MasterToLocal(glob_pt, local_pt);
   
   Printf("my_pt    = [%e,%e,%e]",my_pt[0],my_pt[1],my_pt[2]);
   Printf("local_pt = [%e,%e,%e]",local_pt[0],local_pt[1],local_pt[2]);
   
   
//    my_pt[0]=0.35*1e-2;my_pt[1]=0;my_pt[2]=1e-1;
   my_pt[0]=0;my_pt[1]=0;my_pt[2]=1.1*1e-1;
   // 2nd step
   gGeoManager->SetCurrentPoint(my_pt[0],my_pt[1],my_pt[2]);
   line->SetNextPoint(my_pt[0],my_pt[1],my_pt[2]);
   nextNode = gGeoManager->Step(kFALSE);
   if (!gGeoManager->IsOutside()) Printf("%s",nextNode->GetName());
   gGeoManager->MasterToLocal(glob_pt, local_pt);
   Printf("my_pt    = [%e,%e,%e]",my_pt[0],my_pt[1],my_pt[2]);
   Printf("local_pt = [%e,%e,%e]",local_pt[0],local_pt[1],local_pt[2]);
   
   cmp->AddElement(line);
   cmp->CloseCompound();
   

   gEve->Redraw3D(kTRUE);
}
void ShowEventFake(TString fname) {


   TEveManager::Create();
   
   TGeoNode* node = gGeoManager->GetTopNode();
   TEveGeoTopNode* en = new TEveGeoTopNode(gGeoManager, node);
//    en->SetVisLevel(100);
   en->GetNode()->GetVolume()->SetVisibility(kFALSE);
   
   gEve->AddGlobalElement(en);


   TEveCompound* cmp = new TEveCompound;
   cmp->SetMainColor(kGreen);
   gEve->AddElement(cmp);
   cmp->OpenCompound();
   
   
//    Double_t initPiont[] = {0.0,0.0,0.0};
//    Double_t initDirection[] = {0.0,0.0,1.0};
//    gGeoManager->InitTrack(initPiont,initDirection);
   
   TFile *f = TFile::Open(fname.Data(), "READ");
   TTree *tree = (TTree*) f->Get("cpSimTree");
   CpParticle *part = 0;
//   tree->Print();
   tree->SetBranchAddress("cpParticles", &part);
   
   CpTrack *track;
   for(Long64_t iEvent=0;iEvent<tree->GetEntries();iEvent++) {
    tree->GetEntry(i);
    part->Print();
    for ( Long64_t iParticle=0; iParticle<part->GetNumberOfTracks();iParticle++ ) {
      track = (CpTrack*) part->GetTrack(iParticle);
      TEveLine *line = new TEveLine;
      line->SetMainColor(iParticle);
      for (Long64_t iPoint=0; iPoint<nunParticles;iPoint++){
	CpPoint *p = (CpPoint*) track->GetPoint(iPoint);
	line->SetNextPoint(p->GetX(),p->GetY(),p->GetZ());
      }
      cmp->AddElement(line);
    }
  }

  cmp->CloseCompound();
   
  gEve->Redraw3D(kTRUE);
}
